const { src, dest } = require("gulp")
const { output, themes } = require("./options")
const uglify = require("gulp-uglify")
const babel = require("gulp-babel")
const cleanCSS = require("gulp-clean-css")
const postcss = require("gulp-postcss")
const autoprefixer = require("autoprefixer")

// 处理 index js
const minifyIndex = async () =>
  src("./index.js")
    .pipe(
      babel({
        presets: ["@babel/preset-env"]
      })
    )
    .pipe(uglify())
    .pipe(dest(`${output}`))

// 处理 theme js
const minifyJs = async () =>
  themes.forEach(v => {
    src(`theme/${v}/${v}.js`)
      .pipe(
        babel({
          presets: ["@babel/preset-env"]
        })
      )
      .pipe(uglify())
      .pipe(dest(`${output}/theme/${v}`))
  })

// 处理 theme css
const minifyCSS = async () =>
  themes.forEach(v => {
    src(`theme/${v}/${v}.css`)
      // .pipe(autoprefixer())
      .pipe(postcss([autoprefixer()]))
      .pipe(cleanCSS({ compatibility: "ie8" }))
      .pipe(dest(`${output}/theme/${v}`))
  })

// 处理 assets
const assets = async () => src("assets/**/*").pipe(dest(`${output}/assets`))

module.exports = [minifyIndex, minifyJs, minifyCSS, assets]
